package com.mtiiot.fstcpclient;

import com.mtiiot.fscore.bean.DfsFileInfo;
import com.mtiiot.fscore.bean.MetadataInfo;
import com.mtiiot.fscore.bean.elasticsearch.metadata.NettyMetadata;
import com.mtiiot.fscore.bean.email.MailAttachmentBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/24.
 */
public class TestNetty {
    private static NettyService1 nettyService1;
    private static FastdfsService1 fastdfsService1;
    private static EmailService1 emailService1;
    private static ElasticsearchService1 elasticsearchService1;
    private static KafkaConsumerService1 kafkaConsumerService1;
    private static NIPStatService1 nipStatService1;

    public static void testNettyService(){
        //String[] code = {"N_YQ_QCM_RSZZ","N_YQ_TES_RPFB","N_BX_STA_HSZZ","N_YQ_TRA_RSZZ"};
        String[] code = {"N_YQ_TES_RPFB"};
        String[] id = {
                "4af4e88a4e570dd6014e62978031620c"
        };

        /*String[] dfsId = {
                "group1/M00/28/E0/CnVFylfHmZiAaq9eAAAFJf6r53o77.json",
                "group1/M00/29/03/CnXXwVfHm0qAD4VrAAAFJf6r53o74.json",
                "group1/M00/28/E1/CnVFylfHmzGAHQZXAAAFJf6r53o89.json"
        };*/
        String[] dfsId = {
                "group1/M00/29/8E/CnVFylfJAymAKQT3AAAFJf6r53o71.json"
        };
/*        String[] dfsId = {
                "group1/M00/29/AF/CnXXwVfJAySANw9gAAAFJf6r53o76.json",
                "group1/M00/29/8E/CnVFylfJAymAKQT3AAAFJf6r53o71.json"
        };*/
        // (MetadataInfo metadataInfo, String deviceId, String seqNo, String code, String ext, byte[] file)

        for (int i = 0; i < id.length;i++){
            String deviceId = id[i];
            String seqNo = "1";
            String ext = "json";
            //String bytes = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><head custom-id=\"40281281401ec28f01401ec53f690003\" box-id=\"0205010000\" instrument-id=\"20240\" process-date=\"20160425094934\" /><content>30-32-30-34-32-35-37-35-30-31-20-20-30-2E-30-20-20-30-2E-35-20-20-30-2E-37-20-20-30-2E-33-20-20-31-2E-39-20-20-35-2E-37-20-39-32-2E-30-20-20-30-2E-30-20-20-30-2E-30-20-20-36-2E-39-30-30-30-30-30-31-20-2D-20-30-31-20-20-20-20-20-20-20-20-20-20-20-0D</content></root>";
            String bytes = dfsId[i];
            MetadataInfo metadataInfo = new MetadataInfo();
            metadataInfo.setAccessKey("88888888");
            metadataInfo.setSysId("8f1b37d5aa8b45699ef0d46473d1de44");
            metadataInfo.setL1Name("TestNetty");
            metadataInfo.setL2Name("testNettyService");
            metadataInfo.setL3Name("test");
            metadataInfo.setHost("192.168.2.168");
            nettyService1.storeFile(metadataInfo,deviceId,seqNo,code[i],ext,bytes.getBytes());
        }
    }

    /**
     * 将文件上传到分布式文件系统
     * */
    public static void uploadFile(){
        for (int i = 0; i < 1; i++){
            //String bytes = "{\"custom_id\":\"f9479ebb49757874014979d5fe25003d\",\"box_id\":\"f8975ec6e2f14e1bb19687ddde4af72b\",\"instrument_id\":\"4af4e88a4e570dd6014e62978031620c\",\"process_date\":\"20160810144256\",\"content\":\"02-31-48-7C-5C-5E-26-7C-31-34-37-31-35-30-30-7C-7C-63-6F-62-61-73-20-38-30-30-30-5E-31-2E-30-34-7C-7C-7C-7C-7C-68-6F-73-74-7C-52-53-55-50-4C-7C-50-7C-31-7C-32-30-31-36-30-38-31-30-30-38-33-35-32-38-7C-0D-50-7C-31-7C-7C-50-61-74-49-44-33-7C-7C-50-61-72-6B-65-72-5E-42-69-6C-6C-7C-7C-31-39-38-38-31-32-33-31-7C-4D-7C-0D-4F-7C-31-7C-33-31-34-30-36-7C-30-5E-35-30-30-36-35-5E-31-5E-5E-53-31-5E-53-43-5E-6E-6F-74-7C-5E-5E-5E-36-33-5E-31-7C-52-7C-32-30-31-36-30-38-31-30-30-38-31-35-34-31-7C-32-30-31-36-30-38-31-30-30-38-31-35-34-31-7C-7C-7C-7C-4E-7C-7C-7C-7C-31-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-46-7C-0D-43-7C-31-7C-49-7C-5E-5E-5E-5E-7C-47-7C-0D-52-7C-31-7C-5E-5E-5E-36-33-2F-31-2F-6E-6F-74-7C-39-37-2E-37-34-7C-6E-67-2F-6D-4C-7C-5E-17-39-44-0D-0A-02-32-54-45-43-48-5C-5E-4E-4F-52-4D-5C-5E-43-52-49-54-5C-5E-55-53-45-52-7C-7C-7C-46-7C-7C-6C-62-5E-53-59-53-54-45-4D-7C-32-30-31-36-30-38-31-30-30-38-31-37-30-31-7C-32-30-31-36-30-38-31-30-30-38-33-35-33-32-7C-65-36-30-32-5E-31-5E-4D-55-31-23-65-36-30-32-23-31-23-31-5E-33-5E-30-7C-0D-43-7C-31-7C-49-7C-30-7C-49-7C-0D-4C-7C-31-7C-4E-7C-0D-03-31-44-0D-0A-04\",\"analyze_name\":\"INS_Cobas_E601\"}";
            String bytes = "{\"custom_id\":\"40281281401ec28f01401ec53f690003\",\"box_id\":\"2bd021d7a7c74f55b932a4d43de8c034\",\"instrument_id\":\"7cec5d9871c247018bdf65d685eeacbd\",\"process_date\":\"20161205101049\",\"content\":\"H4sIAAAAAAAAAHVRSW7EMAx7EQHH1mIf02b6/yeVlDOHKVLAB0KmKIpqHd5gE3YiA2EYA3ahf/2t\\r\\n54J1ZEN0xAv2QrA+MKgQyCm+s3cV84IZ7Ci8EE+9vtQbsx7BheGIb2H2jqYu7wISj/91TljCQkxi\\r\\nf/fSQ5zlYRZz3VKqb5yqy/NRuDzHzxOuvSIRBzzlMzloKis5dOVAD+4Ye1/O9fKwVOdSWZxxiKDE\\r\\nlnRohpUPz9uniUN8p/c0l3fZWXntTiY5/L11dlaHbse70A8XybqXcl5SIBafsy608Qv4LdZGCQIA\\r\\nAA==\",\"analyze_name\":\"INS_Database_Odbc\",\"is_zip\":\"true\"}";
            MetadataInfo metadataInfo = new MetadataInfo();
            metadataInfo.setAccessKey("88888888");
            metadataInfo.setSysId("8f1b37d5aa8b45699ef0d46473d1de44");
            metadataInfo.setL1Name("TestNetty");
            metadataInfo.setL2Name("testNettyService");
            metadataInfo.setL3Name("test");
            metadataInfo.setHost("192.168.2.168");
            String dfsId = fastdfsService1.storeFile(metadataInfo,"json",bytes.getBytes());
            System.out.println(dfsId);
        }

    }
    public static void downloadFile(){
        //for (int i = 0; i < 1; i++){
            //String bytes = "{\"custom_id\":\"40281281401ec28f01401ec53f690003\",\"box_id\":\"2bd021d7a7c74f55b932a4d43de8c034\",\"instrument_id\":\"7cec5d9871c247018bdf65d685eeacbd\",\"process_date\":\"20161205101049\",\"content\":\"H4sIAAAAAAAAAHVRSW7EMAx7EQHH1mIf02b6/yeVlDOHKVLAB0KmKIpqHd5gE3YiA2EYA3ahf/2t\\r\\n54J1ZEN0xAv2QrA+MKgQyCm+s3cV84IZ7Ci8EE+9vtQbsx7BheGIb2H2jqYu7wISj/91TljCQkxi\\r\\nf/fSQ5zlYRZz3VKqb5yqy/NRuDzHzxOuvSIRBzzlMzloKis5dOVAD+4Ye1/O9fKwVOdSWZxxiKDE\\r\\nlnRohpUPz9uniUN8p/c0l3fZWXntTiY5/L11dlaHbse70A8XybqXcl5SIBafsy608Qv4LdZGCQIA\\r\\nAA==\",\"analyze_name\":\"INS_Database_Odbc\",\"is_zip\":\"true\"}";
        String bytes = "{\"custom_id\":\"f9479ebb49757874014979d5fe25003d\",\"box_id\":\"cc7d7205c66040848e305af6761872b7\",\"instrument_id\":\"20192\",\"process_date\":\"20161206051434\",\"content\":\"02-31-48-7C-5C-5E-26-7C-7C-7C-4E-47-5F-4C-49-53-7C-7C-7C-7C-7C-4C-49-53-5F-49-44-7C-7C-50-7C-31-0D-50-7C-31-7C-7C-7C-7C-7C-7C-7C-55-0D-4F-7C-31-7C-34-34-31-34-32-34-36-37-36-36-30-30-5E-30-30-34-37-5E-44-7C-7C-5E-5E-5E-46-54-34-7C-52-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-7C-46-0D-52-7C-31-7C-5E-5E-5E-46-54-34-5E-5E-5E-31-5E-44-4F-53-45-7C-31-35-2E-34-35-7C-70-6D-6F-6C-2F-4C-7C-7C-7C-7C-46-7C-7C-7C-7C-32-30-31-36-31-32-30-36-30-36-35-39-31-33-0D-52-7C-32-7C-5E-5E-5E-46-54-34-5E-5E-5E-31-5E-43-4F-46-46-7C-31-2E-30-30-7C-70-6D-6F-6C-2F-4C-7C-7C-7C-7C-46-7C-7C-7C-7C-32-30-31-36-31-32-30-36-30-36-35-39-31-33-0D-52-7C-33-7C-5E-5E-5E-46-54-34-5E-5E-5E-31-5E-52-4C-55-7C-34-35-38-38-39-7C-7C-7C-7C-7C-46-7C-17-43-43-0D-0A-02-32-7C-7C-7C-32-30-31-36-31-32-30-36-30-36-35-39-31-33-0D-4C-7C-31-0D-03-38-36-0D-0A-04\",\"analyze_name\":\"INS_AdviaCentaur_XP\"}";
        MetadataInfo metadataInfo = new MetadataInfo();
        metadataInfo.setAccessKey("88888888");
        metadataInfo.setSysId("8f1b37d5aa8b45699ef0d46473d1de44");
        metadataInfo.setL1Name("TestNetty");
        metadataInfo.setL2Name("testNettyService");
        metadataInfo.setL3Name("test");
        metadataInfo.setHost("192.168.2.168");
        String dfsId = fastdfsService1.storeFile(metadataInfo,"json",bytes.getBytes());
        /*DfsFileInfo dfsId =  fastdfsService1.getFile(metadataInfo,"group100/M00/00/45/eBrqnVhFWnqAAcqxAAACM2TLoSM48.json");
        System.out.println(dfsId.getExt());
        System.out.println(dfsId.getFileSize());*/
            System.out.println(dfsId);
       // }

    }

    public static void sendEmail(){
        /**
         * 发送带附件的邮件
         * @param subject 邮件主题
         * @param sendHtml 邮件内容
         * @param receiveUser 收件人地址邮箱列表
         * @param fileName 邮件所带附件的文件名
         * @param ext 邮件所带附件的文件后缀
         * @param attachmentDesc 邮件所带附件的说明
         * @param attachment 邮件所带附件的二进制数组
         */

        String subject = "月季度计划表";
        String sendHtml = "hello,world!!";
        String[] receiveUser = {"627047574@qq.com","xuanyaorong@mtiiot.com","13610368174@163.com","yaorongxuan@wistronits.com"};
        //String[] receiveUser = {"627047574@qq.com"};
        String fileName = "月季度计划表";
        String ext = "docx";
        String attachmentDesc = "月季度计划表";
        String emailContent = "月季度计划表";
        byte[] attachment = emailContent.getBytes();

        List<MailAttachmentBean> list = new ArrayList<MailAttachmentBean>();
        MailAttachmentBean attachmentBean = new MailAttachmentBean();
        attachmentBean.setAttachments(attachment);
        attachmentBean.setFileName(fileName);
        attachmentBean.setExt(ext);
        list.add(attachmentBean);
        list.add(attachmentBean);
        list.add(attachmentBean);


        MetadataInfo metadataInfo = new MetadataInfo();
        metadataInfo.setAccessKey("88888888");
        metadataInfo.setSysId("8f1b37d5aa8b45699ef0d46473d1de44");
        metadataInfo.setL1Name("TestSendEmail");
        metadataInfo.setL2Name("TestSendEmail");
        metadataInfo.setL3Name("TestSendEmail");
        metadataInfo.setHost("192.168.2.168");

        emailService1.sendAttachmentEmail(metadataInfo,subject,sendHtml,receiveUser,attachmentDesc,list);

        //emailService1.sendEmailByHtml(metadataInfo,receiveUser,subject,sendHtml);
    }

    // 测试es数据
    public static void testEsData(){
        MetadataInfo metadataInfo = new MetadataInfo();
        metadataInfo.setAccessKey("88888888");
        metadataInfo.setSysId("8f1b37d5aa8b45699ef0d46473d1de44");
        metadataInfo.setHost("192.168.2.168");


        /////////////////////////////////////////////
        String index = "index-fs-metadata";
        String type = "type-fs-metadata-fastdfs";
        NettyMetadata metadata = new NettyMetadata();
        metadata.setDeviceId("26e7a916547a4d91b146a4f29d41b0ac");
        metadata.setCode("N_YQ_TES_RPFB");
        metadata.setSeqNo("1");
        metadata.setDfsId("group2/M00/00/4D/Chpn8Ffe5pKATGrfAAAEyMUEg3A42.json");
        metadata.setExt("json");
        metadata.setFileType("netty");
        metadata.setFileSize(2.0);
        // 设置重传的次数
        metadata.setRetransmissionCount(0);
        // 设置重传的时间
        metadata.setRetransmissionTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        // 调用接口
        elasticsearchService1.createIndex(metadataInfo,index,type,metadata);
        //elasticsearchService1.createIndexByAsy(metadataInfo,index,type,metadata);

    }

    // 测试康源接口
    public static void testNIP(){
        MetadataInfo metadataInfo = new MetadataInfo();
        metadataInfo.setAccessKey("88888888");
        metadataInfo.setSysId("8f1b37d5aa8b45699ef0d46473d1de44");
        metadataInfo.setHost("192.168.2.168");

        String labId = "40281281401ec28f01401ec53f690003" ;
        String deptId = "16";
//        long startTime = 1480499851000;
//        long endTime = ;
        String startTimeStr = "2016-11-29 00:00:00";
        String endTimeStr = "2016-12-02 23:59:00";



        long startTime = changeDate(startTimeStr);
        long endTime = changeDate(endTimeStr);
        /*if  (nipStatService1 != null) {
            nipStatService1.getSpeSummaryData(metadataInfo, labId, deptId,endTime , startTime);
        }*/
        Map<String,Integer> map = nipStatService1.getSpeSummaryData(metadataInfo, "", "",startTime ,endTime );

        for (Map.Entry<String,Integer> m :map.entrySet()){
            System.out.println(m.getKey()+"="+m.getValue());
        }

    }

    public static long changeDate(String time){
        long l = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(time);
            l = date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l;
    }


    public static void main(String[] args) {
        String path = "D:\\ideaworkspace\\mtiiot\\FSTcpClient\\src\\main\\resources\\applicationContext.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:"+path);
        //nettyService1 = context.getBean("nettyService",NettyService1.class);
        fastdfsService1 = context.getBean("fastdfsService1",FastdfsService1.class);
        //testNettyService();
        //uploadFile();
        downloadFile();

        // 测试邮件的附件接口
        //emailService1 = context.getBean("emailService1",EmailService1.class);
        //sendEmail();

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        // 2016-11-1
        // 测试es数据处理接口方法
        /*elasticsearchService1 = context.getBean("esService1",ElasticsearchService1.class);
        for (int i = 0; i < 10; i++){
            testEsData();
            System.out.println(i);
        }*/


        /*kafkaConsumerService1 = context.getBean("kafkaConsumerService1",KafkaConsumerService1.class);

        kafkaConsumerService1.consumeMessage("111","aaa","sss");*/

        /*nipStatService1 = context.getBean("nipStatService1",NIPStatService1.class);
        testNIP();*/
        System.out.println("sssssssss");

    }

}
